package util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;


public class DBUtils {
	
	private static Connection conn;
	
	// 硬编码（开发禁用）
//	private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
//	private static final String URL = "jdbc:mysql://localhost:3306/scott_db?charset=utf8&serverTimezone=GMT%2B8";
//	private static final String USER_NAME = "root";
//	private static final String PASSWORD = "123456";
	
	static {
		try {
			// 属性类
			Properties prop = new Properties();
			prop.load(new FileInputStream("./src/conf/jdbc.properties"));
			String driver = prop.getProperty("jdbc.driver");
			String url = prop.getProperty("jdbc.url");
			String user = prop.getProperty("jdbc.user");
			String password = prop.getProperty("jdbc.password");
			
			Class.forName(driver);
			conn = DriverManager.getConnection(url,user,password);
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	// 提供公开的方法返回连接
	public static Connection getConnection() {
		return conn;
	}
}
